$(function () {
    //加载表格数据
    methods.getTableData();
});

var data = {
    //页面对话框
    dialog: {
        show: false,
        loading: false,
        disabled: false,
        title: "信息"
    },
    //分页
    paging: {
        currentPage: 1,
        pageSize: 10,
        total: 0,
        tableData: []
    },
    //搜索
    search: {
        name: ""
    },
    //当前对象
    currentObj: {}
};

var methods = {
    //角色授权
    roleAuthorization: function (row) {
        dialog.iframe("角色授权", "700px", "590px",
            "/system/menu/menu_tree?roleId=" + row.id,
            function (index, layero) {
                var iframeWin = window[layero.find('iframe')[0]['name']];
                iframeWin.methods.getSelectKeys();
                var selectIds = iframeWin.data.selectIds.join(",");
                var oldIds = iframeWin.data.oldIds;
                if (selectIds != oldIds) {
                    ajax.post("/system/menu/saveMenuRole", {
                        roleId: row.id,
                        menuIds: selectIds
                    }, function (res) {
                        layer.close(index);
                        layer.msg("操作成功");
                    })
                } else {
                    layer.msg("请修改后提交");
                }
            });
    },
    //查看角色成员
    showRoleUser: function (row) {
        var url = "/system/user/role_user?id=" + row.id + "&name=" + row.name;
        dialog.iframeInfo("100%", "100%", url, function () {
            methods.getTableData();
        });
    },
    //搜索角色
    searchRole: function () {
        data.paging.currentPage = 1;
        methods.getTableData();
    },
    //修改角色
    updateRoleDialog: function (row) {
        data.dialog.title = "修改【" + row.name + "】角色";
        data.currentObj = JSON.parse(JSON.stringify(row));
        data.dialog.disabled = true;
        data.dialog.show = true;
    },
    //添加角色
    saveOrUpdateRole: function () {
        if (!data.currentObj.name) {
            layer.msg("角色名称不能为空");
            return;
        }
        if (!data.currentObj.code) {
            layer.msg("角色编码不能为空");
            return;
        }
        if (!data.currentObj.priority) {
            layer.msg("优先级不能为空");
            return;
        }
        data.dialog.loading = true;
        ajax.post("/system/user/saveOrUpdateRole", data.currentObj, function (res) {
            data.dialog.loading = false;
            if (res.code == 1) {
                layer.msg(res.msg);
                data.dialog.show = false;
                methods.getTableData();
            } else {
                layer.alert(res.msg);
            }
        })
    },
    //获取表格数据
    getTableData: function () {
        ajax.post("/system/user/getRolePagingData", {
            currentPage: data.paging.currentPage,
            pageSize: data.paging.pageSize,
            name: data.search.name
        }, function (res) {
            data.paging.total = res.data.total;
            data.paging.tableData = res.data.rows;
        })

    },
    //处理页面大小变化
    handleSizeChange: function (val) {
        data.paging.pageSize = val;
        data.paging.currentPage = 1;
        methods.getTableData();
    },
    //处理当前页面变化
    handleCurrentChange: function (val) {
        data.paging.currentPage = val;
        methods.getTableData();
    },
    //处理对话框关闭
    handleDialogClose: function () {
        data.dialog.loading = false;
        data.dialog.disabled = false;
        data.dialog.title = "信息";
        data.currentObj = {};
    }

};
var vm = new Vue({data: data, methods: methods}).$mount("#app");
